﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using Quebec.Logging.Services;

namespace Quebec.Silverlight.Logging
{
	/// <summary>
	/// A helper class around the WCF LoggingService.    
	/// Any method call on this class are delegate async to the WCF LoggingService.     
	/// </summary>
	public sealed class Logger : ClientBase<ILoggingService>
	{
		private readonly string _applicationName;

		/// <summary>
		/// Creates a new instance of the Logger
		/// </summary>
		/// <param name="binding">The binding that the Logging Service use</param>
		/// <param name="address">The endpoint address to the Logging Server</param>
		/// <param name="applicationName">A string that uniquely identifies this application</param>
		public Logger(System.ServiceModel.Channels.Binding binding, EndpointAddress address, string applicationName)
			: base(binding, address)
		{
			if (string.IsNullOrEmpty(applicationName))
			{
				throw new ArgumentException("applicationName parameter cannot be empty.", applicationName);
			}

			_applicationName = applicationName;
		}

		/// <summary>
		/// Closes the logger and release the resources
		/// </summary>
		public void CloseLogger()
		{
			try
			{
				this.InnerChannel.BeginClose(TimeSpan.FromSeconds(5), null, null);
				System.Diagnostics.Debug.WriteLine("The ChannelFactory was closed ok");
			}
			catch
			{
				System.Diagnostics.Debug.WriteLine("Error while closing the ChannelFactory");
				//ok. Nothing we can do here.
			}
		}

		/// <summary>
		/// Log a message object with the Debug level.
		/// </summary>
		/// <param name="message">The message to log</param>
		public void DebugAsync(string message)
		{
			try
			{
				var a = Channel.BeginDebug(_applicationName, message, null, null);
				//    Channel.EndDebug(a);
			}
			catch (Exception e)
			{
			}

		}

		/// <summary>
		/// Log a message object with the Info level.
		/// </summary>
		/// <param name="message">The message to log<</param>
		public void InfoAsync(object message)
		{
			Channel.BeginInfo(_applicationName, message, null, null);
		}

		/// <summary>
		/// Log a message object with the Error level.
		/// </summary>
		/// <param name="message">The message to log<</param>
		public void ErrorAsync(object message)
		{
			Channel.BeginError(_applicationName, message, null, null);
		}

		/// <summary>
		/// Log a message object with the Fatal level.
		/// </summary>
		/// <param name="message">The message to log<</param>
		public void FatalAsync(object message)
		{
			Channel.BeginFatal(_applicationName, message, null, null);
		}

		/// <summary>
		/// Log a message object with the Warn level.
		/// </summary>
		/// <param name="message">The message to log<</param>
		public void WarnAsync(object message)
		{
			Channel.BeginWarn(_applicationName, message, null, null);
		}
	}
}
